import pandas as pd
from sklearn.preprocessing import LabelEncoder

#数据集编码
def myEncode():
    # 数据集
    train_df = pd.read_excel("携程航班处理数据.xlsx")
    # 删除Price列
    data = train_df.drop(["price"], axis=1)
    # 处理分类数据
    train_categorical_data = data.select_dtypes(exclude=['int64', 'float','int32'])
    # 获取所有列名
    column_names = train_categorical_data.columns.tolist()
    # 遍历每一列
    for column in column_names:
        df = pd.DataFrame()
        # 使用LabelEncoder进行标签编码
        le = LabelEncoder()
        df[column] = train_categorical_data[column]
        df[column+ '_encoded'] = le.fit_transform(train_categorical_data[column])
        df.drop_duplicates(keep='first', inplace=True)
        # 写入Excel文件
        df.to_excel(f'./encode/{column}_encoded.xlsx', index=False)
